package com.sojpt.boot.controller.admin;

import java.util.Arrays;
import java.util.Date;

import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.sojpt.boot.ui.iview.IViewController;
import com.sojpt.kit.IdKit;
import com.sojpt.model.TybkCooperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 *
 * @author Sohnny
 *
 */
@RestController
@RequestMapping("/admin/cooperation")
public class TybkCooperationAdminController extends IViewController {

    @RequestMapping("/list")
    public void list() {

        TybkCooperation tybkCooperation = getModel(TybkCooperation.class, "tybkCooperation");

        SqlPara sqlPara = Db.getSqlPara("admin_cooperation.select", getKv(tybkCooperation));
        Page<Record> page = Db.paginate(pageNumber(), pageSize(), sqlPara);
        renderJson(page);
    }

    @RequestMapping("/save")
    public void save() {
        TybkCooperation tybkCooperation = getModel(TybkCooperation.class, "tybkCooperation");
        tybkCooperation.setId(IdKit.generatorID());
        tybkCooperation.setCreateTime(new Date());
        tybkCooperation.setDeleted(0);
        if(tybkCooperation.save()) {
            renderJson(Ret.ok());
        } else {
            renderJson(Ret.fail());
        }
    }

    @RequestMapping("/update")
    public void update() {
        TybkCooperation tybkCooperation = getModel(TybkCooperation.class, "tybkCooperation");
        tybkCooperation.setModificationTime(new Date());
        if(tybkCooperation.update()) {
            renderJson(Ret.ok());
        } else {
            renderJson(Ret.fail());
        }
    }

    @RequestMapping("/delete")
    public void delete() {
        TybkCooperation tybkCooperation = getModel(TybkCooperation.class, "tybkCooperation");
        tybkCooperation.setDeletionTime(new Date());
        tybkCooperation.setDeleted(1);
        if(tybkCooperation.update()) {
            renderJson(Ret.ok());
        } else {
            renderJson(Ret.fail());
        }
    }

    @RequestMapping("/batchDelete")
    public void batchDelete() {
        int a = Db.update("UPDATE tybk_cooperation SET deleted=1,deletion_time=NOW() WHERE id in" + "(" + getPara("ids") + ")");
        if (a > 0) {
            renderJson(Ret.ok());
        } else {
            renderJson(Ret.fail());
        }
    }

}